<template>
    <f7-page @page:beforein="pageBeforeIn">
        <f7-navbar>
            <f7-nav-left>
                <f7-link @click="back">
                    <f7-icon icon="icon-back" style="margin-left:15px;"></f7-icon>
                </f7-link>
            </f7-nav-left>
            <f7-nav-title title="检查批次"></f7-nav-title>
            <f7-nav-right v-if="isOnline" style="font-size: 16px;">
                <f7-link @click="addBatch">
                    <span class="tabbar-label">新建批次</span>
                    <f7-icon material="add"></f7-icon>
                </f7-link>
            </f7-nav-right>
        </f7-navbar>
        <f7-list>
            <f7-list-item v-for="(batch, i) in batchList"
                :key="batch.id" :title="batch.name" :radio="true"
                :checked="batch.id == batchId" @click="changeBatch(batch)">
                <f7-button slot="after" @click.stop="openBatchDetail(batch)">详情</f7-button>
            </f7-list-item>
        </f7-list>
    </f7-page>
</template>

<script>
import networkEvent from '@/mixins/networkEvent';
import back from '@/mixins/back.js';
import Common from "@/utils/Common.js";
import DateUtil from "@/utils/DateUtil.js";
import events from 'events';
import store from '@/store';
import { getUser, setUser, clearUser } from '@/utils/auth';
import { getStore, setStore, clearStore } from '@/utils/storage';
import keys from '@/utils/rememberKeys';

export default {
    mixins: [networkEvent, back],
    store: store,
    data: function() {
        return {
        	batchId: null,
            batchList: []
        };
    },
    methods: {
    	pageBeforeIn: function() {
            var self = this;
            self.$f7.preloader.show();
            self.batchId = getStore(keys.localeCheckBatchIdKey);
            var batchList = [];
            let sql = 
                "select t.* " +
                "from tb_checkquality_batch t " +
                "left join tb_bid_section p " +
                "on t.bid_section_id = p.id " +
                "where p.project_id = '" + getStore(keys.projectIdKey) + "' " +
                "and t.is_close = 0 " +
                "and (t.in_charge_by = '" + getUser().userId + "' or t.id in ( " +
                "    select batch_id from tb_batch_user where user_id = '" + getUser().userId + "' " +
                ")) ";
            Common.executeSql(sql, []).then((result) => {
            	let rs = result.rs;
                let currentBatchIndex = -1;
                for(var i = 0; i < rs.rows.length; i++) {
                    var row = rs.rows.item(i);
                    var batch = self.convertCheckQualityBatch(row);
                    batchList.push(batch);
                }
            }).then(() => {
            	self.batchList = batchList;
                self.$f7.preloader.hide();
            }).catch((error) => {
            	console.error(error);
            	self.$f7.preloader.hide();
            });
    	},
        addBatch: function() {
            var self = this;
            var router = this.$f7router.navigate('/Workbench/LocaleCheck/BatchModify');
        },
        openBatchDetail: function(batch) {
            var self = this;
            var router = this.$f7router.navigate('/Workbench/LocaleCheck/BatchDetail?batchId=' + batch.id);
        },
        convertCheckQualityBatch: function(row) {
            var batch = {};
            batch.id = row.id;
            batch.name = row.name;
            batch.repairDeadline = row.repair_deadline;
            batch.bidSectionId = row.bid_section_id;
            batch.inChargeBy = row.in_charge_by;
            batch.checked = false;
            return batch;
        },
        changeBatch: function(batch) {
        	this.batchId = batch.id;
        	this.back();
        }
    },
    watch: {
    	'batchId': function(newVal, oldVal) {
    		setStore(keys.localeCheckBatchIdKey, this.batchId);
    	}
    }
}
</script>

<style scoped>

</style>